﻿using Common;
using DevExpress.XtraEditors;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SpecialPopulationAssessmentForm
{
    public partial class AddSPAF : Form
    {
        //窗体构造函数
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id;
        public AddSPAF(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            this.PatientInfo = patientInfo;
            this.LoginUser = loginUser;

            //绑定数据
            BindInfo(this.Id);
            ElectronicSignature();
        }
        public AddSPAF(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);

            //绑定数据
            BindInfo(this.Id);
            ElectronicSignature();
        }
        /// <summary>
        /// 初始化加载印章图片
        /// </summary>
        private void ElectronicSignature()
        {
            ElectronicSignature es = new ElectronicSignature(LoginUser);
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, textqianming, picElectronicSignature, textqianming.Location);
        }

        private void BindInfo(string id)
        {
            textqianming.Text = this.LoginUser.UserName;
            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.SpaForm, 1);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (string.IsNullOrEmpty(this.Id))
                    record = list[0];
                else
                    record = list.Find(p => p.Id == Convert.ToInt32(this.Id));
                this.Id = record.Id.ToString();
                var NursingFormData = record.NursingFormData;
                Spaf spaf = new Spaf();
                spaf = XmlUtil.XMLToObject<Spaf>(NursingFormData, Encoding.UTF8);

                chkChangqitengtongrq0.Checked = spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun.Split('#')[0].Equals("1");
                chkChangqitengtongrq1.Checked = spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun.Split('#')[1].Equals("1");

                checkchulicuoshichangqi1.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[0].Equals("1");
                checkchulicuoshichangqi2.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[1].Equals("1");
                checkchulicuoshichangqi3.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[2].Equals("1");
                checkchulicuoshichangqi4.Checked = spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi.Split('#')[3].Equals("1");

                checkduihuanzheyingxiang1.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[0].Equals("1");
                checkduihuanzheyingxiang2.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[1].Equals("1");
                checkduihuanzheyingxiang3.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[2].Equals("1");
                checkduihuanzheyingxiang4.Checked = spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang.Split('#')[3].Equals("1");

                checkhuanzheduizhitongyao1.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[0].Equals("1");
                checkhuanzheduizhitongyao2.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[1].Equals("1");
                checkhuanzheduizhitongyao3.Checked = spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing.Split('#')[2].Equals("1");

                checkyizhounei1.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[0].Equals("1");
                checkyizhounei2.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[1].Equals("1");
                checkyizhounei3.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[2].Equals("1");
                checkyizhounei4.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[3].Equals("1");
                checkyizhounei5.Checked = spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao.Split('#')[4].Equals("1");

                checkyongyaoxiaoguo1.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[0].Equals("1");
                checkyongyaoxiaoguo2.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[1].Equals("1");
                checkyongyaoxiaoguo3.Checked = spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo.Split('#')[2].Equals("1");

                checkyuanyin1.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[0].Equals("1");
                checkyuanyin2.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[1].Equals("1");
                checkyuanyin3.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[2].Equals("1");
                checkyuanyin4.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[3].Equals("1");
                checkyuanyin5.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[4].Equals("1");
                checkyuanyin6.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[5].Equals("1");
                checkyuanyin7.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[6].Equals("1");
                checkyuanyin8.Checked = spaf.ChangQiTengTongHuanZhe.YuanYin.Split('#')[7].Equals("1");

                checkchulicuoshizisha1.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[0].Equals("1");
                checkchulicuoshizisha2.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[1].Equals("1");
                checkchulicuoshizisha3.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[2].Equals("1");
                checkchulicuoshizisha4.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[3].Equals("1");
                checkchulicuoshizisha5.Checked = spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha.Split('#')[4].Equals("1");

                checkweixdianyinsu1.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[0].Equals("1");
                checkweixdianyinsu2.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[1].Equals("1");
                checkweixdianyinsu3.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[2].Equals("1");
                checkweixdianyinsu4.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[3].Equals("1");
                checkweixdianyinsu5.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[4].Equals("1");
                checkweixdianyinsu6.Checked = spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu.Split('#')[5].Equals("1");

                checkzisha1.Checked = spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang.Split('#')[0].Equals("1");
                checkzisha2.Checked = spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang.Split('#')[1].Equals("1");

                checkchulicuoshilinzhong1.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[0].Equals("1");
                checkchulicuoshilinzhong2.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[1].Equals("1");
                checkchulicuoshilinzhong3.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[2].Equals("1");
                checkchulicuoshilinzhong4.Checked = spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong.Split('#')[3].Equals("1");

                checkqiangjiu1.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[0].Equals("1");
                checkqiangjiu2.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[1].Equals("1");
                checkqiangjiu3.Checked = spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu.Split('#')[2].Equals("1");

                checkhuanzheqingxu1.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[0].Equals("1");
                checkhuanzheqingxu2.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[1].Equals("1");
                checkhuanzheqingxu3.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[2].Equals("1");
                checkhuanzheqingxu4.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[3].Equals("1");
                checkhuanzheqingxu5.Checked = spaf.LinZhongHuanZhe.HuanZheQingXu.Split('#')[4].Equals("1");

                checkhuanzhexuqiu1.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[0].Equals("1");
                checkhuanzhexuqiu2.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[1].Equals("1");
                checkhuanzhexuqiu3.Checked = spaf.LinZhongHuanZhe.HuanZheXuQiu.Split('#')[2].Equals("1");

                checkjiarenxuqiu.Checked = spaf.LinZhongHuanZhe.JiaRenXuQiu.Equals("1");

                textjibingzhenduan.Text = spaf.LinZhongHuanZhe.JiBingZhenDuan;

                checklinzhonghuanzhe1.Checked = spaf.LinZhongHuanZhe.LZHZ.Split('#')[0].Equals("1");
                checklinzhonghuanzhe2.Checked = spaf.LinZhongHuanZhe.LZHZ.Split('#')[1].Equals("1");

                checkzhuyaozhengzhuang1.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[0].Equals("1");
                checkzhuyaozhengzhuang2.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[1].Equals("1");
                checkzhuyaozhengzhuang3.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[2].Equals("1");
                checkzhuyaozhengzhuang4.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[3].Equals("1");
                checkzhuyaozhengzhuang5.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[4].Equals("1");
                checkzhuyaozhengzhuang6.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[5].Equals("1");
                checkzhuyaozhengzhuang7.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[6].Equals("1");
                checkzhuyaozhengzhuang8.Checked = spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang.Split('#')[7].Equals("1");

                checkchulicuoshijingshen1.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[0].Equals("1");
                checkchulicuoshijingshen2.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[1].Equals("1");
                checkchulicuoshijingshen3.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[2].Equals("1");
                checkchulicuoshijingshen4.Checked = spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen.Split('#')[3].Equals("1");

                checkfuzaxiangxiang1.Checked = spaf.JinShenZhangAiHuanZhe.FuFaXianXiang.Split('#')[0].Equals("1");
                checkfuzaxiangxiang2.Checked = spaf.JinShenZhangAiHuanZhe.FuFaXianXiang.Split('#')[1].Equals("1");

                checkjiazushi1.Checked = spaf.JinShenZhangAiHuanZhe.JiaZuShi.Split('#')[0].Equals("1");
                checkjiazushi2.Checked = spaf.JinShenZhangAiHuanZhe.JiaZuShi.Split('#')[1].Equals("1");

                checkjingshenzhangai1.Checked = spaf.JinShenZhangAiHuanZhe.JingShenZhangAi.Split('#')[0].Equals("1");
                checkjingshenzhangai2.Checked = spaf.JinShenZhangAiHuanZhe.JingShenZhangAi.Split('#')[1].Equals("1");

                timemocifabingshijian.EditValue = spaf.JinShenZhangAiHuanZhe.MoCiFaBingShiJian;

                checkpeihezhiliao1.Checked = spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao.Split('#')[0].Equals("1");
                checkpeihezhiliao2.Checked = spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao.Split('#')[1].Equals("1");

                timeshoucifabingshijian.Text = spaf.JinShenZhangAiHuanZhe.ShouCiFaBingShiJian;

                checkxinlizhuangkuang1.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[0].Equals("1");
                checkxinlizhuangkuang2.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[1].Equals("1");
                checkxinlizhuangkuang3.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[2].Equals("1");
                checkxinlizhuangkuang4.Checked = spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang.Split('#')[3].Equals("1");

                checkzhengguizhiliao1.Checked = spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao.Split('#')[0].Equals("1");
                checkzhengguizhiliao2.Checked = spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao.Split('#')[1].Equals("1");

                textxiaoguo.Text = spaf.JinShenZhangAiHuanZhe.XiaoGuo;

                checkchulicuoshixiujiu1.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[0].Equals("1");
                checkchulicuoshixiujiu2.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[1].Equals("1");
                checkchulicuoshixiujiu3.Checked = spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu.Split('#')[2].Equals("1");

                checkxinliyali1.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[0].Equals("1");
                checkxinliyali2.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[1].Equals("1");
                checkxinliyali3.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[2].Equals("1");
                checkxinliyali4.Checked = spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu.Split('#')[3].Equals("1");

                checkxujiu1.Checked = spaf.XuJiuHuanZhe.XuJiu.Split('#')[0].Equals("1");
                checkxujiu2.Checked = spaf.XuJiuHuanZhe.XuJiu.Split('#')[1].Equals("1");

                checkxujiuqingkuang1.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[0].Equals("1");
                checkxujiuqingkuang2.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[1].Equals("1");
                checkxujiuqingkuang3.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[2].Equals("1");
                checkxujiuqingkuang4.Checked = spaf.XuJiuHuanZhe.XuJiuQingKuang.Split('#')[3].Equals("1");

                checkxujiuyuanyin1.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[0].Equals("1");
                checkxujiuyuanyin2.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[1].Equals("1");
                checkxujiuyuanyin3.Checked = spaf.XuJiuHuanZhe.XuJiuYunaYin.Split('#')[2].Equals("1");

                checkjiujingzhongdu1.Checked = spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing.Split('#')[0].Equals("1");
                checkjiujingzhongdu2.Checked = spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing.Split('#')[1].Equals("1");

                //吸毒
                checkchulicuoshixidu1.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[0].Equals("1");
                checkchulicuoshixidu2.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[1].Equals("1");
                checkchulicuoshixidu3.Checked = spaf.XiDuHuanZhe.ChuLioCuo_XiDu.Split('#')[2].Equals("1");

                checkfuxixianxiang1.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[0].Equals("1");
                checkfuxixianxiang2.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[1].Equals("1");
                checkfuxixianxiang3.Checked = spaf.XiDuHuanZhe.FuXiXianXiang.Split('#')[2].Equals("1");

                checkhbsag1.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[0].Equals("1");
                checkhbsag2.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[1].Equals("1");
                checkhbsag3.Checked = spaf.XiDuHuanZhe.HBsAg.Split('#')[2].Equals("1");

                checkkanghiv1.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[0].Equals("1");
                checkkanghiv2.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[1].Equals("1");
                checkkanghiv3.Checked = spaf.XiDuHuanZhe.KangHIV.Split('#')[2].Equals("1");

                checkxiduhuanzhe1.Checked = spaf.XiDuHuanZhe.XDHZ.Split('#')[0].Equals("1");
                checkxiduhuanzhe2.Checked = spaf.XiDuHuanZhe.XDHZ.Split('#')[1].Equals("1");

                checkxidufangshi1.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[0].Equals("1");
                checkxidufangshi2.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[1].Equals("1");
                checkxidufangshi3.Checked = spaf.XiDuHuanZhe.XiDuFangShi.Split('#')[2].Equals("1");

                checkxidupinlv1.Checked = spaf.XiDuHuanZhe.XiDuPinLv.Split('#')[0].Equals("1");
                checkxidupinlv2.Checked = spaf.XiDuHuanZhe.XiDuPinLv.Split('#')[1].Equals("1");

                checkxinliyalixidu1.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[0].Equals("1");
                checkxinliyalixidu2.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[1].Equals("1");
                checkxinliyalixidu3.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[2].Equals("1");
                checkxinliyalixidu4.Checked = spaf.XiDuHuanZhe.XinLiYaLi_XiDu.Split('#')[3].Equals("1");

                checkzhengguijiedu1.Checked = spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao.Split('#')[0].Equals("1");
                checkzhengguijiedu2.Checked = spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao.Split('#')[1].Equals("1");

                //签名
                textqianming.Text = spaf.HuShiQianMing;
                dateEditriqi.EditValue = Convert.ToDateTime(spaf.RiQi).ToString("yyyy-MM-dd");
            }
            else if (ret.Code == 2)
            {
                InsertMode(false);
                return;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }

        #region 刷新
        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo(this.Id);
        }
        #endregion

        #region  新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            InsertMode(true);
        }
        #endregion

        #region  保存
        private void btn_Preservation_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateEditriqi.Text))
                dateEditriqi.DateTime = DateTime.Now;
            Spaf spaf = new Spaf();
            spaf.ChangQiTengTongHuanZhe.ChangQiTengTongRenQun =
                (chkChangqitengtongrq0.Checked ? 1 : 0) + "#" +
                (chkChangqitengtongrq1.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.ChuLiCuoShi_ChangQi =
                (checkchulicuoshichangqi1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshichangqi4.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.DuiHuanZheYingXiang =
                (checkduihuanzheyingxiang1.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang2.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang3.Checked ? 1 : 0) + "#" +
                (checkduihuanzheyingxiang4.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.HuanZheDuiZhiTongYaoChengYinKenNegXing =
                (checkhuanzheduizhitongyao1.Checked ? 1 : 0) + "#" +
                (checkhuanzheduizhitongyao2.Checked ? 1 : 0) + "#" +
                (checkhuanzheduizhitongyao3.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YiZhouNeiCenYongYao =
                (checkyizhounei1.Checked ? 1 : 0) + "#" +
                (checkyizhounei2.Checked ? 1 : 0) + "#" +
                (checkyizhounei3.Checked ? 1 : 0) + "#" +
                (checkyizhounei4.Checked ? 1 : 0) + "#" +
                (checkyizhounei5.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YongYaoHouXiaoGuo =
                (checkyongyaoxiaoguo1.Checked ? 1 : 0) + "#" +
                (checkyongyaoxiaoguo2.Checked ? 1 : 0) + "#" +
                (checkyongyaoxiaoguo3.Checked ? 1 : 0);

            spaf.ChangQiTengTongHuanZhe.YuanYin =
                (checkyuanyin1.Checked ? 1 : 0) + "#" +
                (checkyuanyin2.Checked ? 1 : 0) + "#" +
                (checkyuanyin3.Checked ? 1 : 0) + "#" +
                (checkyuanyin4.Checked ? 1 : 0) + "#" +
                (checkyuanyin5.Checked ? 1 : 0) + "#" +
                (checkyuanyin6.Checked ? 1 : 0) + "#" +
                (checkyuanyin7.Checked ? 1 : 0) + "#" +
                (checkyuanyin8.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.ChuLioCuoShi_ZiSha =
                (checkchulicuoshizisha1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha4.Checked ? 1 : 0) + "#" +
                (checkchulicuoshizisha5.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.WeiXianYinSu =
                (checkweixdianyinsu1.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu2.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu3.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu4.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu5.Checked ? 1 : 0) + "#" +
                (checkweixdianyinsu6.Checked ? 1 : 0);

            spaf.ZiShaQingXiangHuanZhe.ZiShaQingXiang =
                (checkzisha1.Checked ? 1 : 0) + "#" +
                (checkzisha2.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.ChuLioCuo_LinZhong =
                (checkchulicuoshilinzhong1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshilinzhong4.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheJiShuLinZhongQiangJiuTaiDu =
                (checkqiangjiu1.Checked ? 1 : 0) + "#" +
                (checkqiangjiu2.Checked ? 1 : 0) + "#" +
                (checkqiangjiu3.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheQingXu =
                (checkhuanzheqingxu1.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu2.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu3.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu4.Checked ? 1 : 0) + "#" +
                (checkhuanzheqingxu5.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.HuanZheXuQiu =
                (checkhuanzhexuqiu1.Checked ? 1 : 0) + "#" +
                (checkhuanzhexuqiu2.Checked ? 1 : 0) + "#" +
                (checkhuanzhexuqiu3.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.JiaRenXuQiu = (checkjiarenxuqiu.Checked ? 1 : 0).ToString();

            spaf.LinZhongHuanZhe.JiBingZhenDuan = textjibingzhenduan.Text;

            spaf.LinZhongHuanZhe.LZHZ =
                (checklinzhonghuanzhe1.Checked ? 1 : 0) + "#" +
                (checklinzhonghuanzhe2.Checked ? 1 : 0);

            spaf.LinZhongHuanZhe.ZhuYaoZhengZhuang =
                (checkzhuyaozhengzhuang1.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang2.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang3.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang4.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang5.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang6.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang7.Checked ? 1 : 0) + "#" +
                (checkzhuyaozhengzhuang8.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ChuLioCuo_JingShen =
                (checkchulicuoshijingshen1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen3.Checked ? 1 : 0) + "#" +
                (checkchulicuoshijingshen4.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.FuFaXianXiang =
                (checkfuzaxiangxiang1.Checked ? 1 : 0) + "#" +
                (checkfuzaxiangxiang2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.JiaZuShi =
                (checkjiazushi1.Checked ? 1 : 0) + "#" +
                (checkjiazushi2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.JingShenZhangAi =
                (checkjingshenzhangai1.Checked ? 1 : 0) + "#" +
                (checkjingshenzhangai2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.MoCiFaBingShiJian = timemocifabingshijian.Text;

            spaf.JinShenZhangAiHuanZhe.PiHeZhiLiao =
                (checkpeihezhiliao1.Checked ? 1 : 0) + "#" +
                (checkpeihezhiliao2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ShouCiFaBingShiJian = timeshoucifabingshijian.Text;

            spaf.JinShenZhangAiHuanZhe.XinLiZhuangKuang =
                (checkxinlizhuangkuang1.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang2.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang3.Checked ? 1 : 0) + "#" +
                (checkxinlizhuangkuang4.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.ZhengGuiZhiLiao =
                (checkzhengguizhiliao1.Checked ? 1 : 0) + "#" +
                (checkzhengguizhiliao2.Checked ? 1 : 0);

            spaf.JinShenZhangAiHuanZhe.XiaoGuo = textxiaoguo.Text;

            spaf.XuJiuHuanZhe.ChuLioCuo_XuJiu =
                (checkchulicuoshixiujiu1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixiujiu2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixiujiu3.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XinLiYaLi_XuJiu =
                (checkxinliyali1.Checked ? 1 : 0) + "#" +
                (checkxinliyali2.Checked ? 1 : 0) + "#" +
                (checkxinliyali3.Checked ? 1 : 0) + "#" +
                (checkxinliyali4.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiu =
                (checkxujiu1.Checked ? 1 : 0) + "#" +
                (checkxujiu2.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuQingKuang =
                (checkxujiuqingkuang1.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang2.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang3.Checked ? 1 : 0) + "#" +
                (checkxujiuqingkuang4.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuYunaYin =
                (checkxujiuyuanyin1.Checked ? 1 : 0) + "#" +
                (checkxujiuyuanyin2.Checked ? 1 : 0) + "#" +
                (checkxujiuyuanyin3.Checked ? 1 : 0);

            spaf.XuJiuHuanZhe.XuJiuZhongDuXingJiBing =
                (checkjiujingzhongdu1.Checked ? 1 : 0) + "#" +
                (checkjiujingzhongdu2.Checked ? 1 : 0);


            //吸毒开始
            spaf.XiDuHuanZhe.ChuLioCuo_XiDu =
                (checkchulicuoshixidu1.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixidu2.Checked ? 1 : 0) + "#" +
                (checkchulicuoshixidu3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.FuXiXianXiang =
                (checkfuxixianxiang1.Checked ? 1 : 0) + "#" +
                (checkfuxixianxiang2.Checked ? 1 : 0) + "#" +
                (checkfuxixianxiang3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.HBsAg =
                (checkhbsag1.Checked ? 1 : 0) + "#" +
                (checkhbsag2.Checked ? 1 : 0) + "#" +
                (checkhbsag3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.KangHIV =
                (checkkanghiv1.Checked ? 1 : 0) + "#" +
                (checkkanghiv2.Checked ? 1 : 0) + "#" +
                (checkkanghiv3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XDHZ =
                (checkxiduhuanzhe1.Checked ? 1 : 0) + "#" +
                (checkxiduhuanzhe2.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XiDuFangShi =
                (checkxidufangshi1.Checked ? 1 : 0) + "#" +
                (checkxidufangshi2.Checked ? 1 : 0) + "#" +
                (checkxidufangshi3.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XiDuPinLv =
                (checkxidupinlv1.Checked ? 1 : 0) + "#" +
                (checkxidupinlv2.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.XinLiYaLi_XiDu =
                (checkxinliyalixidu1.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu2.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu3.Checked ? 1 : 0) + "#" +
                (checkxinliyalixidu4.Checked ? 1 : 0);

            spaf.XiDuHuanZhe.ZhengGuiJieDuZhiLiao =
                (checkzhengguijiedu1.Checked ? 1 : 0) + "#" +
                 (checkzhengguijiedu2.Checked ? 1 : 0);

            //签名开始 护士签名字段 如果 是新增的并且输入框为空的话 使用系统当前护士签名

            spaf.HuShiQianMing = (string.IsNullOrEmpty(this.Id) && string.IsNullOrEmpty(textqianming.Text)) ? LoginUser.UserName : textqianming.Text;
            spaf.RiQi = dateEditriqi.Text;

            spaf.FormId = (int)FormType.SpaForm;
            spaf.FormName = EnumHelper.GetDescription(FormType.SpaForm);
            record.NursingFormData = XmlUtil.ObjectToXML(spaf, Encoding.UTF8);
            //表单ID
            record.FormId = (int)FormType.SpaForm;
            record.FormName = EnumHelper.GetDescription(FormType.SpaForm);
            //住院号
            record.PatientId = PatientInfo.PATIENT_ID;
            //住院次数
            record.VisitId = PatientInfo.VISIT_ID;
            //患者姓名
            record.PatientName = PatientInfo.PATIENT_NAME;
            //床号
            record.BedNo = PatientInfo.BED_NO;
            //当前患者科室ID
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;
            //当前患者科室名称
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;
            //当前患者病区ID
            record.AreaId = PatientInfo.NURSE_CELL_CODE;
            //当前患者病区名称
            record.AreaName = PatientInfo.NURSE_CELL_NAME;
            record.AssessmentDate = dateEditriqi.DateTime;
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));

            if (ret.Code == 1)
            {
                if (Base.ESSwitch.Equals("on"))//电签开始
                {
                    //TODO:电签接口提交
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, EnumHelper.GetDescription(FormType.SpaForm) + (record.Id > 0 ? "-更新" : "-新增"), EnumHelper.GetDescription(FormType.SpaForm), record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool flag = es.GetElectronicSignatureImage(es, record.NursingFormData, extraInfo, ((int)FormType.SpaForm).ToString(), EnumHelper.GetDescription(FormType.SpaForm), PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID);
                    if (!flag) { MessageBox.Show("电子签名失败"); return; }
                }//电签结束
                MessageBox.Show(record.Id == 0 ? "新增成功" : "更新成功");
                if (record.Id > 0) return;
                //新增成功后绑定数据
                this.Id = string.Empty;
                BindInfo(this.Id);
            }
            else
            {
                MessageBox.Show(ret.M);
            }
            //点击保存完后，关闭新增页面
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Id))
            {
                MessageBox.Show("当前为新增模式，无法操作");
                return;
            }
            PrintSPAF pss = new PrintSPAF(LoginUser, PatientInfo, this.Id);
            pss.Show();
        }
        #endregion

        #region 删除
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Id)) { MessageBox.Show("当前为新增模式，无法操作"); return; }
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) != DialogResult.OK) return;

            var ret = formService.DeleteNursingFormRecord(this.Id);
            MessageBox.Show(ret.M);
            if (ret.Code != 1) return;

            this.Id = string.Empty;
            BindInfo(this.Id);

        }
        /// <summary>
        /// 没有记录单的时候
        /// </summary>
        private void ShowClose()
        {
            MessageBox.Show(string.Format("该患者没有《{0}》 \r\n 自动转入新增界面", EnumHelper.GetDescription(FormType.SpaForm)),
                "关闭提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            InsertMode(false);
        }

        private void InsertMode(bool openTips)
        {
            if (openTips && MessageBox.Show("是否新增该表单", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                return;
            foreach (Control c in groupControl1.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl2.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl3.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl4.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl5.Controls)
                ControlValueClear(c);
            foreach (Control c in groupControl6.Controls)
                ControlValueClear(c);
            this.Id = string.Empty;
            record = new NursingFormRecord();
            dateEditriqi.DateTime = DateTime.Now;
        }

        /// <summary>
        /// 清除控件值
        /// </summary>
        /// <param name="c"></param>
        private static void ControlValueClear(Control c)
        {
            if (c is DateEdit)
                ((DateEdit)c).Text = string.Empty;
            if (c is TextEdit)
                ((TextEdit)c).Text = string.Empty;
            if (c is CheckEdit)
                ((CheckEdit)c).Checked = false;
            if (c is RadioGroup)
                ((RadioGroup)c).SelectedIndex = -1;
            if (c is ComboBoxEdit)
                ((ComboBoxEdit)c).SelectedIndex = -1;
        }
        #endregion

        #region 历史记录
        private void btnHis_Click(object sender, EventArgs e)
        {
            HistorySPAF hss = new HistorySPAF(this.PatientInfo);
            hss.ShowDialog();
            if (hss.DialogResult != DialogResult.OK) return;
            if (string.IsNullOrEmpty(hss.Id)) return;
            this.Id = hss.Id;
            BindInfo(this.Id);
        }
        #endregion

        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
    }
}